package com.bawei.day18_mediaplayer2;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.lauzy.freedom.library.Lrc;
import com.lauzy.freedom.library.LrcHelper;
import com.lauzy.freedom.library.LrcView;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;

public class LyricActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt_play;
    private LrcView lrc;
    private MediaPlayer mediaPlayer = new MediaPlayer();

    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lyric);
        initView();
        initPlayer();
        initTimer();


    }

    private void initTimer() {
        timer  = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //歌词跟着歌曲滚动
                int currentPosition = mediaPlayer.getCurrentPosition();
                lrc.updateTime(currentPosition);

            }
        },0,100);
    }

    private void initPlayer() {
        if(mediaPlayer != null){
            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource("/sdcard/Music/白举纲-我是一只鱼.mp3");
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer.start();

                        //加载歌词
                        List<Lrc> lrcs = LrcHelper.parseLrcFromFile(new File("/sdcard/Music/白举纲-我是一只鱼.lrc"));
                        lrc.setLrcData(lrcs);
                        //滑动歌词
                        lrc.setOnPlayIndicatorLineListener(new LrcView.OnPlayIndicatorLineListener() {
                            @Override
                            public void onPlay(long time, String content) {
                                mediaPlayer.seekTo((int) time);
                            }
                        });


                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void initView() {
        bt_play = (Button) findViewById(R.id.bt_play);
        lrc = (LrcView) findViewById(R.id.lrc);

        bt_play.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_play:
                if(mediaPlayer != null){
                    if(mediaPlayer.isPlaying()){
                        mediaPlayer.pause();
                    }else{
                        mediaPlayer.start();
                    }
                }

                break;
        }
    }
}
